package world3d.world.entities;

import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureData;
import com.jogamp.opengl.util.texture.TextureIO;
import com.sun.scenario.effect.impl.BufferUtil;
import world3d.world.Entity;

import javax.imageio.ImageIO;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLProfile;
import javax.media.opengl.glu.GLU;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

public class SphereOnXYEntity extends Entity {
	private GLU glu;
	private float radius = .378f;
	private int radiusSphere = 2000;
	private int radiusIncVector = 1;
	private Texture solarPanelTexture;
	private double x =0;
	private double y=0;
	private double z = 0;

	public SphereOnXYEntity() {
		x = y = z = 0;
	}

	public SphereOnXYEntity(double x, double y, double z) {
		this.x = x;
		this.y = y;
		this.z = z;
	}

	@Override
	public void display() {
//		x+=.001;
		uvSphere(gl2, .4, 22, 22, true, x, y, z);
	}
	public void uvSphere(GL2 gl, double radius, int slices, int stacks, boolean makeTexCoords, double x, double y, double z) {
		if (radius <= 0)
			throw new IllegalArgumentException("Radius must be positive.");
		if (slices < 3)
			throw new IllegalArgumentException("Number of slices must be at least 3.");
		if (stacks < 2)
			throw new IllegalArgumentException("Number of stacks must be at least 2.");
		for (int j = 0; j < stacks; j++) {
			double latitude1 = (Math.PI/stacks) * j - Math.PI/2;
			double latitude2 = (Math.PI/stacks) * (j+1) - Math.PI/2;
//			latitude1  = latitude1/radius;
//			latitude2  = latitude2/radius;
			double sinLat1 = Math.sin(latitude1);
			double cosLat1 = Math.cos(latitude1);
			double sinLat2 = Math.sin(latitude2);
			double cosLat2 = Math.cos(latitude2);
			gl.glBegin(GL2.GL_QUAD_STRIP);
			solarPanelTexture.enable(gl);
			solarPanelTexture.bind(gl);
//			gl.glBegin(GL2.GL_FILL);
			for (int i = 0; i <= slices; i++) {
				double longitude = ((2*Math.PI/slices) * i);///radius;
				double sinLong = Math.sin(longitude);
				double cosLong = Math.cos(longitude);
				double x1 = cosLong * cosLat1*radius + x;
				double y1 = sinLong * cosLat1*radius + y;
				double z1 = sinLat1*radius + z;
				double x2 = cosLong * cosLat2*radius + x;
				double y2 = sinLong * cosLat2*radius + y;
				double z2 = sinLat2*radius + z;
				gl.glNormal3d(x2,y2,z2);
				if (makeTexCoords)
					gl.glTexCoord2d(1.0/slices * i, 1.0/stacks * (j+1));
				gl.glVertex3d(x2,y2,z2);
				gl.glNormal3d(x1,y1,z1);
				if (makeTexCoords)
					gl.glTexCoord2d(1.0/slices * i, 1.0/stacks * j);
				gl.glVertex3d(x1,y1,z1);
			}
			gl.glActiveTexture(GL.GL_TEXTURE0);
			gl.glEnd();

//			gl.glTexImage2D();
//			glu.gluQuadricTexture(earth, true);
//			glu.gluTexture(earth, true);

		}
	}
	@Override
	public void init(GLAutoDrawable glAutoDrawable) {
		super.init(glAutoDrawable);
		try {
			InputStream stream = getClass().getResourceAsStream("/earth_texture.jpg");
			TextureData data = TextureIO.newTextureData(GLProfile.getDefault(), stream, false, "jpg");
			solarPanelTexture = TextureIO.newTexture(data);
		}
		catch (IOException exc) {
			exc.printStackTrace();
			System.exit(2);
		}

		try {
			BufferedImage image = ImageIO.read(getClass().getClassLoader().getResource("earth_texture.jpg"));
			DataBufferByte dbb = (DataBufferByte)image.getRaster().getDataBuffer();
			byte[] data = dbb.getData();
			ByteBuffer pixels = BufferUtil.newByteBuffer(data.length);
			pixels.put(data);
			pixels.flip();
			gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, 256, 256, 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, pixels);
		} catch (IOException e) {
			e.printStackTrace();
		}

		glu = new GLU();
	}

	@Override
	public void reshape(int x, int y, int width, int height) {}

	@Override
	public void dispose() {

	}
}
